package com.opgea.tutor.web.controller;

import java.io.ByteArrayInputStream;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opgea.tutor.TutorException;
import com.opgea.tutor.domain.model.PrimeFacesFileUploadBean;
import com.opgea.tutor.domain.qualifiers.TutorDocumentType;
import com.opgea.tutor.service.TutorService;

@ManagedBean(name = "imageController")
@RequestScoped
@Component
@Scope(value = "request")
public class ImageController {

    @Autowired
    private TutorService tutorService;

    public StreamedContent getTutorImage() {
	// long tutorId = Long.valueOf((String)
	// FacesUtil.getParameter("tutorId"));
	PrimeFacesFileUploadBean bean = null;
	try {
	    bean = tutorService.findDocumentByType(1L,
		    TutorDocumentType.PERSONAL_IMAGE.toString());
	} catch (TutorException e) {
	    e.printStackTrace();
	}
	if (bean != null && bean.getFile().getContents() != null) {
	    return new DefaultStreamedContent(new ByteArrayInputStream(bean
		    .getFile().getContents()));
	} else {
	    return new DefaultStreamedContent();
	}
    }
}
